package com.qc31.monitorlib.ui.alarms

import autodispose2.autoDispose
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.alarm.AlarmDataUtil
import com.qc31.comlib.entity.HistoryAlarm
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import com.qc31.monitorlib.base.BaseReportViewModel
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.subjects.PublishSubject

/** @Author Jasper @Time 2020/12/9 @Desc todo */
class BaseAlarmViewModel : BaseReportViewModel() {

    var runSpeed = "0"
    var duration = "0"
    var typeIds = ""
    private val alarmSub by lazy { PublishSubject.create<HistoryAlarm>() }
    fun dataObserver(): Observable<HistoryAlarm> {
        return alarmSub.hide()
    }

    override fun isParamsOk(): Boolean {
        if (typeIds.isEmpty()) {
            toast(R.string.hint_choose_alarm_type)
            return true
        }
        if (runSpeed.isEmpty()) {
            runSpeed = "0"
        }

        if (duration.isEmpty()) {
            duration = "0"
        }


        return super.isParamsOk()
    }

    fun getHtml(desc: String, isAdas: Boolean) =
        "<font color='#FF333333' >$desc </font><font color='#3791E9'> ${
            AlarmDataUtil.instance.getChooseTotal(
                isAdas
            )
        }</font><font color='#FF333333'>/${AlarmDataUtil.instance.getTotal(isAdas)}</font>"

    override fun getData(carId: String, startTime: String, endTime: String) {
        toastLoading(true)
        ServiceHelper.getRepository().getHistoryAlarm(
            carId,
            TimeUtil.instance.formatHms(startTime),
            TimeUtil.instance.formatEndHms(endTime),
            typeIds,
            runSpeed,
            TimeUtil.instance.formatTime(duration.toInt() * 60)
        )
            .autoDispose(this).subscribe({
                toastLoading(false)
                if (isHasListData(it.list, R.string.toast_no_about_alarm)) return@subscribe
                alarmSub.onNext(it)
            }, error)
    }


}
